package com.example.springbootrabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * tang 测试优先级队列
 * 应用启动后，会连接到rabbitmq创建对应的队列等
 * 但是，这个类的exchange , queue 都没有自动创建，
 * 但是登录管理页面进行手工创建并绑定后，消息的发送、接收都正常
 */

@Service
public class RabbitMqPriorityInitService {


    // ======================以下是验证优先级Priority的队列======================

    @Bean
    public TopicExchange priorExchange() {
        return new TopicExchange("priorExchange");
    }

    @Bean
    public Queue priorQueue() {
        Map<String, Object> arguments = new HashMap<>();
        //创建优先级队列
        arguments.put("x-max-priority", 10);
        return new Queue("prior.queue", true, false, true, arguments);
    }



    /**
     * 将队列topic与exchange绑定，binding_key为topic.message
     * @return
     */
    @Bean
    public Binding bindingPriorExchangeMessage(Queue priorQueue, TopicExchange priorExchange) {
        return BindingBuilder.bind(priorQueue).to(priorExchange).with("prior.queue");
    }


    // ======================以上是验证prior的队列======================



}
